package com.ruoyi.web.operate.issue.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.operate.issue.service.IIssueApprovalService;
import com.ruoyi.web.operate.stock.domain.Stock;
import com.ruoyi.web.operate.stock.service.IStockService;
import com.ruoyi.web.publish.issue.domain.IssueHistory;
import com.ruoyi.web.publish.issue.service.IIssueHistoryService;
import com.ruoyi.web.publish.resume.domain.Resume;
import com.ruoyi.web.publish.resume.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 发行审批 service
 *
 * @author liuyu
 */
@Service
public class IssueApprovalServiceImpl implements IIssueApprovalService {

    @Autowired
    private IIssueHistoryService iIssueHistoryService;
    @Autowired
    private IResumeService iResumeService;
    @Autowired
    private IStockService iStockService;

    /**
     * 查询发行历史
     *
     * @param id 发行历史主键
     * @return 发行历史
     */
    @Override
    public IssueHistory selectIssueHistoryById(Long id) {
        return iIssueHistoryService.selectIssueHistoryById(id);
    }

    /**
     * 查询待审批的发行历史列表
     *
     * @param issueHistory 发行历史
     * @return 发行历史集合
     */
    @Override
    public List<IssueHistory> selectApprovalIssueHistoryList(IssueHistory issueHistory) {
        List<IssueHistory> issueHistoryList = iIssueHistoryService.selectIssueHistoryList(issueHistory);
        issueHistoryList.forEach(i -> {
            Resume resume = iResumeService.getResumeToBelongs(i.getCreateById());
            if (null != resume) {
                i.setIssuer(resume.getName());
            }
        });
        return issueHistoryList;
    }

    /**
     * 审核发行历史
     *
     * @param issueHistory 发行历史
     * @return 结果
     */
    @Override
    public int approvalIssueHistory(IssueHistory issueHistory) {
        // 审核通过后自动生成股票
        if (null != issueHistory && issueHistory.getVerifyType().equals(2)) {
            Resume resume = iResumeService.getResumeToBelongs(issueHistory.getCreateById());
            Stock stock = null;

            Stock stockQuery = new Stock();
            stockQuery.setOwner(issueHistory.getCreateById());
            List<Stock> stockList = iStockService.selectStockList(stockQuery);
            if (stockList.size() == 0) {
                if (null != resume.getScore()) {
                    stock = new Stock();
                    long timeMillis = System.currentTimeMillis();
                    String timeMillisStr = String.valueOf(timeMillis).substring(0, 5);
                    int randomNum = (int) (Math.random() * 900) + 100;
                    stock.setCode(timeMillisStr + randomNum);
                    stock.setName(resume.getName() + "人才股票" + randomNum);

                    stock.setOpenPrice(BigDecimal.valueOf(resume.getScore()));
                    stock.setLastClosePrice(BigDecimal.valueOf(resume.getScore()));
                    stock.setMaxPrice(BigDecimal.valueOf(resume.getScore()));
                    stock.setMinPrice(BigDecimal.valueOf(resume.getScore()));
                    stock.setLatestPrice(BigDecimal.valueOf(resume.getScore()));
                    stock.setAmount(Long.valueOf(issueHistory.getIssueNumber()));
                    stock.setAmountSum(Long.valueOf(issueHistory.getIssueNumber()));
                    stock.setOwner(issueHistory.getCreateById());
                    stock.setRepurchaseVerify(0);
                    iStockService.insertStock(stock);
                }
            } else if (stockList.size() == 1) {
                if (issueHistory.getIssueType().equals(1)) {
                    stock = stockList.get(0);
                    if (stock.getAmount() > 0) {
                        throw new ServiceException("存在未发行完成的股票，不允许增发股票！");
                    }
                    stock.setAmount(stock.getAmount() + Long.valueOf(issueHistory.getIssueNumber()));
                    stock.setAmountSum(stock.getAmountSum() + Long.valueOf(issueHistory.getIssueNumber()));
                    stock.setRepurchaseVerify(0);
                    if (BigDecimal.valueOf(resume.getScore()).compareTo(stock.getMaxPrice()) == 1) {
                        stock.setMaxPrice(BigDecimal.valueOf(resume.getScore()));
                    }

                    if (BigDecimal.valueOf(resume.getScore()).compareTo(stock.getMinPrice()) == -1) {
                        stock.setMinPrice(BigDecimal.valueOf(resume.getScore()));
                    }
                    iStockService.updateStock(stock);

                } else if (issueHistory.getIssueType().equals(0)) {
                    throw new ServiceException("股票已存在不允许再次初始发行股票！");
                }
            }
        }
        return iIssueHistoryService.updateIssueHistory(issueHistory);
    }
}
